package com.example.mydy.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;
import com.google.android.material.textfield.TextInputEditText;

public class EditUserDialog extends Dialog implements View.OnClickListener {

    private UserInfo user;
    private OnUserEditListener listener;
    private TextInputEditText etUsername, etNickname, etPhone, etBalance, etPassword;
    private Spinner spinnerLevel;
    private Button btnSave, btnCancel;

    public interface OnUserEditListener {
        void onUserEdited(UserInfo user);
    }

    public EditUserDialog(@NonNull Context context, UserInfo user, OnUserEditListener listener) {
        super(context);
        this.user = user;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit_user);

        // 初始化视图
        initViews();
        
        // 设置用户数据
        setUserData();
    }

    private void initViews() {
        etUsername = findViewById(R.id.username_edit);
        etNickname = findViewById(R.id.nickname_edit);
        etPhone = findViewById(R.id.phone_edit);
        etBalance = findViewById(R.id.balance_edit);
        etPassword = findViewById(R.id.password_edit);
        spinnerLevel = findViewById(R.id.spinner_user_level);
        
        btnCancel = findViewById(R.id.btn_cancel);
        btnSave = findViewById(R.id.btn_save);
        
        btnCancel.setOnClickListener(this);
        btnSave.setOnClickListener(this);
    }

    private void setUserData() {
        if (user != null) {
            etUsername.setText(user.getUsername());
            etNickname.setText(user.getNickname());
            etPhone.setText(user.getPhone());
            etBalance.setText(user.getBalance());
            
            // 设置用户等级
            String userLevel = user.getLevel();
            for (int i = 0; i < spinnerLevel.getAdapter().getCount(); i++) {
                if (spinnerLevel.getAdapter().getItem(i).toString().equals(userLevel)) {
                    spinnerLevel.setSelection(i);
                    break;
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        
        if (id == R.id.btn_cancel) {
            dismiss();
        } else if (id == R.id.btn_save) {
            saveUserData();
        }
    }

    private void saveUserData() {
        // 获取输入数据
        String username = etUsername.getText().toString().trim();
        String nickname = etNickname.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String balanceStr = etBalance.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String level = spinnerLevel.getSelectedItem().toString();
        
        // 验证数据
        if (username.isEmpty() || nickname.isEmpty()) {
            Toast.makeText(getContext(), "用户名和昵称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        
        try {
            double balance = 0.0;
            if (!balanceStr.isEmpty()) {
                balance = Double.parseDouble(balanceStr);
            }
            
            // 更新用户信息
            if (user == null) {
                user = new UserInfo();
            }
            
            user.setUsername(username);
            user.setNickname(nickname);
            user.setPhone(phone);
            user.setBalance(String.valueOf(balance));
            user.setLevel(level.equals("全部") ? "普通" : level);
            
            // 回调更新
            if (listener != null) {
                listener.onUserEdited(user);
            }
            
            dismiss();
        } catch (NumberFormatException e) {
            Toast.makeText(getContext(), "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }
} 